package com.atguigu;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.alibaba.ververica.cdc.debezium.StringDebeziumDeserializationSchema;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
//import org.apache.flink.runtime.executiongraph.restart.RestartStrategy;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.runtime.state.memory.MemoryStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FlinkCDC {

    public static void main(String[] args) throws Exception {

        //1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //1.1 开启CK并指定状态后端为FS    memory  fs  rocksdb
//        env.setStateBackend(new FsStateBackend("hdfs://cdh04:8020/gmall-flink-210325/ck"));
        env.setStateBackend(new MemoryStateBackend());
        env.enableCheckpointing(5000L);
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(10000L);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(3000);

        //env.setRestartStrategy(RestartStrategies.fixedDelayRestart());

        //2.通过FlinkCDC构建SourceFunction并读取数据
        DebeziumSourceFunction<String> sourceFunction = MySQLSource.<String>builder()
                .hostname("cdh01")
                .port(3306)
                .username("root")
                .password("123456Aa.")
                .databaseList("amon")
//                .tableList("")   //如果不添加该参数,则消费指定数据库中所有表的数据.如果指定,指定方式为db.table
                .deserializer(new StringDebeziumDeserializationSchema())
                .startupOptions(StartupOptions.initial())
                .build();
        DataStreamSource<String> streamSource = env.addSource(sourceFunction);

        //3.打印数据
        streamSource.print();

        //4.启动任务
        env.execute("FlinkCDC");

        /** 示例：
         * SourceRecord{sourcePartition={server=mysql_binlog_source}, sourceOffset={file=mysql-bin.000003, pos=6372494, row=1, snapshot=true}} // offset信息
         * ConnectRecord
         * {
         * topic='mysql_binlog_source.amon.PARTITION_INFO', // 数据库、表名
         * kafkaPartition=null,
         * key=Struct{ID=93},
         * keySchema=Schema{mysql_binlog_source.amon.PARTITION_INFO.Key:STRUCT},
         * value=Struct
         *        {after=Struct{ID=93,NAME=CMON_ATT_LMV_2023_04_15,BASE_TABLE=CMON_ATT_LMV,END_TS=1681603200000,TYPE=0}, // 数据内容
         * 	source=Struct{version=1.4.1.Final,connector=mysql,name=mysql_binlog_source,ts_ms=0,snapshot=true,db=amon,table=PARTITION_INFO,server_id=0,file=mysql-bin.000003,pos=6372494,row=0},
         * 	op=c, // 操作类型，创建、更新、删除等
         * 	ts_ms=1681957766451}, // cdc拉取的时间
         * 	valueSchema=Schema{mysql_binlog_source.amon.PARTITION_INFO.Envelope:STRUCT},
         * 	timestamp=null,
         * 	headers=ConnectHeaders(headers=)
         * }
         */

    }

}
